// pages/caigouList/caigouList.js
var webContext = getApp().globalData.webContext
var index =1
var proCat=''
var city=''
var verity=''
var productCat=[]
var verifyJson = [{
  "code": '',
  "name": "全部"
}, {
  "code": 1,
  "name": "手机号码已验证"
}
]
var result = []
Page({

  /**
   * 页面的初始数据
   */
  data: {
    verifyList: verifyJson ,
    productCatList: productCat,
    noData:false
  },
  bindPickerChange1: function (e) {
    proCat = productCat[e.detail.value].classify_id;
    result=[];
    this.loadData();
  },
  bindPickerChange2: function (e) {
    verity=verifyJson[e.detail.value].code;
    result = [];
    this.loadData();
  },
  chooseCity: function (e) {
    wx.navigateTo({
      url: '../../pages/city/city'
    })
  },
  loadData:function(){
    var _this = this
    wx.request({
      url: webContext + '/ebridge_h5/caigou/getListByPage',//json数据地址
      data: {
        index: index,
        city: city,
        verity: verity,
        proCat: proCat,
        openId:''
      },
      headers: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        console.log(res.data)
        if(index>1){
          if (res.data.data.purchase.length > 0) {

            for (var i = 0; i < res.data.data.purchase.length; i++) {

              result.push(res.data.data.purchase[i]);

            }
            _this.setData({
              list_data: result
            })
          }
        }
        else {

          for (var i = 0; i < res.data.data.purchase.length; i++) {

            result.push(res.data.data.purchase[i]);

          }
          _this.setData({
            list_data: result
          })
       
          if (res.data.data.purchase.length == 0) {
            _this.setData({
              noData: true
            })
          }else{
            _this.setData({
              noData: false
            })
          }
        }
      }
    })
  },
  toDetails:function(e){
    var id = e.currentTarget.dataset.id;
    console.log(e);
    if(id){
      wx.navigateTo({
        url: '../../pages/caigouDetails/caigouDetails?id=' + id
      })
    }
  },
  toReleaseCaigou: function (e) {
    wx.navigateTo({
      url: '../../pages/releaseCaigou/releaseCaigou'
    })
  },
  loadproCat: function () {
    var _this = this
    wx.request({
      url: webContext + '/ebridge_h5/caigou/proCategory',//json数据地址
      headers: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        _this.setData({
          productCatList: res.data.data.pro_classify
        })
        productCat = res.data.data.pro_classify
      }
    })
  },
  toMyCaigou : function(e) {
    wx.navigateTo({
      url: '../../pages/myCaigou/myCaigou'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    index = 1;
    if (options.city) {
      city = options.city
    }
    if (options.refresh==1){
      proCat = ''
      city = ''
      verity = ''
      result = []
    }
    this.loadproCat();
    this.loadData();
 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    index = 1;
    result = [];
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.loadData();
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    index++;
    this.loadData();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})